#!/usr/bin/perl


if ( $#ARGV < 0 ) {
  print "usage: analyze_tcpdump.pl <directory>\n"; 
  exit; 
}

$dir = $ARGV[0]; 
$ping_conf_file=$dir . "/ping.conf"; 
open(ping_conf, "< $ping_conf_file"); 
while(<ping_conf>) 
{ 
    my($line) = $_; 
    chomp($line); 
    
    if ( $line =~ /^\s*$/ || $line =~ /\#/ ) 
    { 
		next; 
    }

    @entries = split(' ', $line); 
    my($ip) = $entries[0]; 
    my($category) = $entries[1]; 
    if ($category eq "PingSender" || 
    	$category eq "DiscoverSender" || 
    	$category eq "PingFuture" || 
    	$category eq "PingSenderTypeCastAck"  ) {         
    	my($type) = $entries[2]; 
		my($session) = $entries[3]; 
		my($endpoint) = $entries[4]; 
		my($rate) = $entries[7]; 		
		analyze($dir, $ip, $type, $session, $endpoint, $rate); 
	} 
}

close(mac_mapping_file);
exit; 


sub analyze { 
  my ($dir, $ip, $type, $session, $endpoint, $rate) = @_; 
  my (@files) = <$dir/tcpdump.*>;
  my ($group, $port) = split(':', $endpoint) ; 
#  print "endpoint=$endpoint port = $port\n"; 
  my $line, $count; 
  foreach $file ( @files ) { 
     if($file =~ /^.*\/tcpdump\.(.*)$/ ) {      
         $server_ip = $1; 
         $tcpdump_wc = "tcpdump -r $file dst net $group | wc -l |"; 
         open(server_log, $tcpdump_wc); 
         $count = -1; 
		 while($line = <server_log>) { 
		    chomp($line);
		 	if ($line =~ /^\d*$/ ) { 
		 	  $count = $line; 
		 	  break; 
		 	}
		 }
		 print "$server_ip $ip $endpoint $type $session $rate $count \n"; 
		 close(server_log); 
	}
  }
} 
		 	                     
  